<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sealplayer
 *
 * @author Samuel
 */
class sendmail_controller extends Controller {
    private $areaList;	//区号列表
    private $areaAddr2ID;
    private $wholeServerHost;    //全局地址

    function _init()
    {
        check_level(50, segment(0), segment(1), segment(2, 'index'));

        $config = get_config();
        $this->wholeServerHost = $config['host_url'];

        $server_list=get_server_list();

        //var_dump($server_list);
        foreach($server_list as $k=>$row) {
            $this->areaList[$row['SvrOutAddr']] = $row['SvrID']."-".$row['SvrName'][1];
            $this->areaAddr2ID[$row['SvrOutAddr']] = $row['SvrID'];
        }
    }

    function index()
    {
        if(is_submit())
        {
            $rowdata = $_POST['row'];
            //var_dump($rowdata);

            $post_data['SessionID']="ewewqw";
            $post_data['SessionKey']="wqqew";

            $game_host=$rowdata['area'];

            foreach ($game_host as $key=>$value){
				$post_data['Type']=(int)$rowdata['enable'];
				if ($post_data['Type'] == 1) {
					$post_data['FightValueL'] = (int)$rowdata['role_fight_value_l'];
					$post_data['FightValueH'] = (int)$rowdata['role_fight_value_h'];
					if ($post_data['FightValueL']>$post_data['FightValueH'] || $post_data['FightValueH'] < 0){
						echo "<script>alert('" . translate('ROLE_FIGHT_VALUE_ERROR') . "')</script>";
						$this->view->assign("arealist", $this->form->select("row[area_host]", $this->areaList, '', array('id' => 'area_host')));
						$this->view->display("seal/mail/send");
						return;
					}
				}else if ($post_data['Type'] == 2){
					$post_data['Level'] = (int)$rowdata['role_level'];
					if ($post_data['Level'] <= 0 || $post_data['Level'] > 100){
						echo "<script>alert('" . translate('ROLE_LEVEL_ERROR') . "')</script>";
						$this->view->assign("arealist", $this->form->select("row[area_host]", $this->areaList, '', array('id' => 'area_host')));
						$this->view->display("seal/mail/send");
						return;
					}
				}else if ($post_data['Type'] == 3){
					if ($rowdata['role_id']==""){
						echo "<script>alert('" . translate('ROLE_ROLE_ID_ERROR') . "')</script>";
						$this->view->assign("arealist", $this->form->select("row[area_host]", $this->areaList, '', array('id' => 'area_host')));
						$this->view->display("seal/mail/send");
						return;
					}
	
					$post_data['TargetIDs'] = array();
					$roleIds = explode(";", $rowdata['role_id']);
					$roleIdCount = count($roleIds);
					for ($i=0;$i<$roleIdCount;$i++){
						//array_push($post_data['TargetIDs'] , (int)($roleIds[$i]));
						array_push($post_data['TargetIDs'] , $roleIds[$i]);
					}
				}

				$post_data['Title']=(string)$rowdata['title'];
				$post_data['MailContent'] = (string)$rowdata['msg'];
				$post_data['Msg'] = (string)$rowdata['msg'];

				$itemcount = 0;
				foreach ($rowdata['item'] as $k=>$v){
					$item=array("ID"=>(int)$v,"Num"=>(int)$rowdata['amount'][$k]);
					$post_data['ItemLst'][$k]=$item;
					if ($itemcount ==0){
						$mailloginfo['itemid0'] = (int)$v;
						$mailloginfo['amount0'] = (int)$rowdata['amount'][$k];
					}else if  ($itemcount ==1){
						$mailloginfo['itemid1'] = (int)$v;
						$mailloginfo['amount1'] = (int)$rowdata['amount'][$k];
					}else if  ($itemcount ==2){
						$mailloginfo['itemid2'] = (int)$v;
						$mailloginfo['amount2'] = (int)$rowdata['amount'][$k];
					}else if  ($itemcount ==3){
						$mailloginfo['itemid3'] = (int)$v;
						$mailloginfo['amount3'] = (int)$rowdata['amount'][$k];
					}
	
					$itemcount += 1;
				}

				//var_dump(json_encode($post_data));
				//var_dump(json_encode_adv($post_data));
				$post_data['SvrID'] = (int)$this->areaAddr2ID[$value];
				$wholeServerHost=$this->wholeServerHost;
				$wholeServerHost="https://".$value."/";///暂时先直接向游戏服务器发指令,跳过gmsvr
				if ($itemcount > 0){
					$strResult = vpost($wholeServerHost.'send_award_to_player',3,json_encode($post_data));
				}else{
					$post_data['Value']=(string)$rowdata['title'];
					$strResult = vpost($wholeServerHost.'send_mail_to_player',3,json_encode($post_data));
				}
	
				//echo $strResult;
				$result=json_decode($strResult,true);
				//操作失败或者连接失败
				if($result['RetCode'] == 0)
				{
					$mailloginfo['areanum'] = $post_data['SvrID'];
					$mailloginfo['mailtype'] = $post_data['Type'];
					$mailloginfo['roleids'] = $rowdata['role_id'];
					$mailloginfo['mail_title'] = $post_data['Title'];
					$mailloginfo['mail_content'] = $post_data['Msg'];
					$mailloginfo['insert_time'] = time();
	
					$result = $this->db->insert ( 'mail_sendlog2', $mailloginfo);
					insert_log(translate('MENU_SEND_OBJECT'),translate('ADD'),NULL,$rowdata);
					echo "<script>alert('".translate('C_TIPS_SUCCESS_TEXT')."')</script>";
				} else {
					//unset($rowdata);
					if ($result['RetCode'] == 1) {
						echo "<script>alert('" . translate('ROLE_FIGHT_VALUE_ERROR') . "')</script>";
					} else if ($result['RetCode'] == 2) {
						echo "<script>alert('" . translate('ROLE_LEVEL_ERROR') . "')</script>";
					} else if ($result['RetCode'] == 3) {
						echo "<script>alert('" . translate('ROLE_ROLE_ID_ERROR') . "')</script>";
					}
				}
            }

        }
        //var_dump($this->areaList);
        //$this->view->assign("arealist", $this->form->select("row[area_host]", $this->areaList, '', array('id' => 'area_host')));   // 原来 为 下拉列表单选 
        $this->view->assign("arealist", $this->areaList);  // 改为 复选框 多选
        $this->view->display("seal/mail/send");
    }

}

?>
